package main

import (
	"time"
	"gowork/serverStream/message"
	"fmt"
	"google.golang.org/grpc"
	"net"
)

//订单服务实现
type OrderServiceImpl struct {

}

//获取订单信息s
func (os *OrderServiceImpl) GetOrderInfos(request *message.OrderList, stream message.OrderService_GetOrderInfosServer) error {
	fmt.Println(" 服务端流 RPC 模式")
	orderMap := map[string]*message.OrderInfo{
		"201907300001": &message.OrderInfo{OrderId: "201907300001", OrderName: "衣服", OrderStatus: "已付款"},
		"201907310001": &message.OrderInfo{OrderId: "201907310001", OrderName: "零食", OrderStatus: "已付款"},
		"201907310002": &message.OrderInfo{OrderId: "201907310002", OrderName: "食品", OrderStatus: "未付款"},
		"201907310003": &message.OrderInfo{OrderId: "201907310003", OrderName: "食品", OrderStatus: "未付款"},
		"201907310004": &message.OrderInfo{OrderId: "201907310004", OrderName: "食品", OrderStatus: "已付款"},
		"201907310005": &message.OrderInfo{OrderId: "201907310005", OrderName: "食品", OrderStatus: "未付款"},
		"201907310006": &message.OrderInfo{OrderId: "201907310006", OrderName: "食品", OrderStatus: "未付款"},
		"201907310007": &message.OrderInfo{OrderId: "201907310007", OrderName: "食品", OrderStatus: "未付款"},
		"201907310008": &message.OrderInfo{OrderId: "201907310008", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310009": &message.OrderInfo{OrderId: "201907310009", OrderName: "食品", OrderStatus: "未付款"},
		"201907310010": &message.OrderInfo{OrderId: "201907310010", OrderName: "食品", OrderStatus: "已付款"},
		"201907310011": &message.OrderInfo{OrderId: "201907310011", OrderName: "食品", OrderStatus: "未付款"},
		"201907310012": &message.OrderInfo{OrderId: "201907310012", OrderName: "食品", OrderStatus: "未付款"},
		"201907310013": &message.OrderInfo{OrderId: "201907310013", OrderName: "食品", OrderStatus: "未付款"},
		"201907310014": &message.OrderInfo{OrderId: "201907310014", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310015": &message.OrderInfo{OrderId: "201907310015", OrderName: "食品", OrderStatus: "未付款"},
		"201907310016": &message.OrderInfo{OrderId: "201907310016", OrderName: "食品", OrderStatus: "未付款"},
		"201907310017": &message.OrderInfo{OrderId: "201907310017", OrderName: "食品", OrderStatus: "未付款"},
		"201907310018": &message.OrderInfo{OrderId: "201907310018", OrderName: "食品", OrderStatus: "未付款"},
		"201907310019": &message.OrderInfo{OrderId: "201907310019", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310020": &message.OrderInfo{OrderId: "201907310020", OrderName: "食品", OrderStatus: "未付款"},
		"201907310021": &message.OrderInfo{OrderId: "201907310021", OrderName: "食品", OrderStatus: "未付款"},
		"201907310022": &message.OrderInfo{OrderId: "201907310022", OrderName: "食品", OrderStatus: "已付款"},
		"201907310023": &message.OrderInfo{OrderId: "201907310023", OrderName: "食品", OrderStatus: "未付款"},
		"201907310024": &message.OrderInfo{OrderId: "201907310024", OrderName: "食品", OrderStatus: "未付款"},
		"201907310025": &message.OrderInfo{OrderId: "201907310025", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310026": &message.OrderInfo{OrderId: "201907310026", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310027": &message.OrderInfo{OrderId: "201907310027", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310028": &message.OrderInfo{OrderId: "201907310028", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310029": &message.OrderInfo{OrderId: "201907310029", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310030": &message.OrderInfo{OrderId: "201907310030", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310031": &message.OrderInfo{OrderId: "201907310031", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310032": &message.OrderInfo{OrderId: "201907310032", OrderName: "食品", OrderStatus: "部分付款"},
		"201907310033": &message.OrderInfo{OrderId: "201907310033", OrderName: "食品", OrderStatus: "部分付款"},
	}

	var index int = 1
	var orders []*message.OrderInfo
	for _, v := range request.OrderList{
		orders = append(orders, orderMap[v.GetOrderId()])
		if (time.Now().Unix() >= v.TimeStamp) {
			if index%3 == 0{
				err := stream.Send(&message.OrderInfoList{OrderInfoList:orders})
				if err != nil{
					return err
				}
				orders = (orders)[0:0]
			}
		}
		index += 1
	}

	if len(orders) > 0 {
		err := stream.Send(&message.OrderInfoList{OrderInfoList:orders})
		if err != nil{
			return err
		}
	}
	return nil
}

func main() {
	server := grpc.NewServer()
	//注册
	message.RegisterOrderServiceServer(server, new(OrderServiceImpl))
	lis, err := net.Listen("tcp", ":8090")
	if err != nil {
		panic(err.Error())
	}
	server.Serve(lis)
}